home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / utilreen / txt2ps / psp.c < prev    next >
C/C++ Source or Header  |  1992-01-23  |  11KB  |  372 lines

  1. /*------------------------------------------------------------------*
  2. *  FILE:       PSP.C                                                *
  3. *  DESC:       Program to print text files on PostScript printers.  *
  4. *  AUTHOR:     Marv Luse, Ithaca Street Software, Inc.              *
  5. *  DATE:       December, 1989                                       *
  6. *  COPYRIGHT:  Use freely as long as authorship is acknowledged.    *
  7. *------------------------------------------------------------------*/
  8.  
  9. #include "stdlib.h"
  10. #include "stdio.h"
  11. #include "string.h"
  12. #include "time.h"
  13.  
  14. /*
  15.  *   Notes:  (1) Program creates an 8-1/2" x 11" page with 3/4"
  16.  *               margins all around.  Top 1/4" and bottom 1/4"
  17.  *               of active page area used for header and footer.
  18.  *
  19.  *           (2) All page coords are in points (72 points = 1 inch)
  20.  *
  21.  *           (3) Page origin (0,0) located at bottom left, yielding:
  22.  *
  23.  *               +--------------------------------------+
  24.  *               |                                      |
  25.  *               |    + (54,738)         (558,738) +    |
  26.  *               |                                      |
  27.  *               |                                      |
  28.  *               /                                      /
  29.  *               :         Page Coordinate Space        :
  30.  *               /              in Points               /
  31.  *               |                                      |
  32.  *               |                                      |
  33.  *               |                                      |
  34.  *               |    + (54,54)           (558,54) +    |
  35.  *               |                                      |
  36.  *               +--------------------------------------+
  37.  *
  38.  *           (4) Control char handling:
  39.  *
  40.  *               Carriage Return (0Dh) causes end-of-line
  41.  *               Line Feed (0Ah) ignored
  42.  *               Form Feed (0Ch) causes end-of-line and end-of-page
  43.  *               Tab Char (09h) expands to TAB_SIZE blanks
  44.  *               All other codes treated as text.
  45.  *
  46.  *           (5) To handle files with ASCII values above 127
  47.  *               the program should be compiled with default
  48.  *               char type of unsigned (/J on MSC, -K on TurboC).
  49.  */
  50.  
  51. #define MAX_LEN     80     /* File Names Buffer Length */
  52. #define HDR_HGT     18     /* Page header space in points */
  53. #define FTR_HGT     18     /* Page footer space in points */
  54. #define LINE_SIZE  132     /* I/O buffer length */
  55. #define TAB_SIZE     5     /* number of blanks per tab */
  56.  
  57. /* special characters */
  58. #define TAB_CH       9
  59. #define FF_CH       12
  60. #define CR_CH       13
  61. #define LF_CH       10
  62.  
  63. /* macro to test for end-of-line */
  64. #define eoln( c )  ( ((c==CR_CH) || (c==FF_CH) || (c==0) ) ? 1 : 0 )
  65.  
  66. /* macro to test for end-of-page */
  67. #define eopg( c, n )  ( ((c==FF_CH) || (n>=LnPerPg)) ? 1 : 0 )
  68.  
  69. /* if not using RIPS Image 4000, undefine RIPS */
  70. #define RIPS 1
  71.  
  72. char FileToPrint[MAX_LEN+1] = "",            /* file to be printed */
  73.      PrintDevice[MAX_LEN+1] = "LPT2",              /* printer port */
  74.      DateAndTime[28],                             /* time and date */
  75.      FontName[MAX_LEN+1]    = "Courier";            /* font to use */
  76.  
  77. int  PtSize  = 11,                           /* text hgt in points */
  78.      LnSize  = 12,                           /* line hgt in points */
  79.      LnPerPg = 0,                                /* lines per page */
  80.      LineNo  = 0,                           /* current line number */
  81.      PageNo  = 0;                           /* current page number */
  82.  
  83. int  XLft, XRgt, XCur,                /* page coordinate variables */
  84.      YTop, YBot, YCur;
  85.  
  86. time_t currentT;                                   /* current time */
  87.  
  88. FILE   *fTxt,                                      /* text file in */
  89.        *fPrt;                                       /* printer out */
  90.  
  91. /*-----------------------------------------------------------------*/
  92.  
  93. /* display a message and exit */
  94.  
  95. void exit_pgm( char *msg, int retv )
  96. {
  97.      printf( "\n%s", msg );
  98.      exit( retv );
  99. }
  100.  
  101. /*-----------------------------------------------------------------*/
  102.  
  103. /* explain program usage */
  104.  
  105. void explain_pgm( void )
  106. {
  107.      printf( "\n" );
  108.      printf( "\nPSP - Program to print text files on PostScript printers" );
  109.      printf( "\n" );
  110.      printf( "\nusage:  PSP  file_to_print  [printer_port]" );
  111.      printf( "\n             file_to_print : path to an ASCII text file" );
  112.      printf( "\n             printer_port  : path to printer [def=LPT2]" );
  113.      printf( "\n" );
  114. }
  115.  
  116. /*-----------------------------------------------------------------*/
  117.  
  118. /* initialize for processing */
  119.  
  120. void start_up( char *f_in, char *f_out )
  121. {
  122.      int i;
  123.  
  124.      fTxt = fopen( f_in, "rt" );
  125.      if( fTxt == NULL )  exit_pgm( "Could not locate input file", 8 );
  126.  
  127.      fPrt = fopen( f_out, "wt" );
  128.      if( fPrt == NULL )  exit_pgm( "Error opening printer", 8 );
  129.  
  130.      time( ¤tT );
  131.      strcpy( DateAndTime, ctime( ¤tT ) );
  132.      /* get rid of trailing newline character */
  133.      i = 0;
  134.      while( DateAndTime[i] >= ' ' ) i++;
  135.      DateAndTime[i] = 0;
  136. }
  137.  
  138. /*-----------------------------------------------------------------*/
  139.  
  140. /* clean up before returning to DOS */
  141.  
  142. void shut_dn( void )
  143. {
  144.      fclose( fTxt );
  145.      fclose( fPrt );
  146.      printf( "\nTotal page(s) printed = %d", PageNo );
  147. }
  148.  
  149. /*-----------------------------------------------------------------*/
  150.  
  151. /* function to write font-select code */
  152.  
  153. void set_font( void )
  154. {
  155.      fprintf( fPrt, "\n/%s findfont", FontName );
  156.      fprintf( fPrt, "\n%d scalefont", PtSize );
  157.      fprintf( fPrt, "\nsetfont" );
  158. }
  159.  
  160. /*-----------------------------------------------------------------*/
  161.  
  162. /* function to draw a line between passed endpoints */
  163.  
  164. void draw_ln( int x1, int y1, int x2, int y2 )
  165. {
  166.      fprintf( fPrt, "\n%d %d moveto %d %d lineto stroke",
  167.                      x1, y1, x2, y2 );
  168. }
  169.  
  170. /*-----------------------------------------------------------------*/
  171.  
  172. /* function to output a string with surrounding parentheses    */
  173. /* special characters are also "escaped" by prefixing with '\' */
  174.  
  175. void write_str( char *str )
  176. {
  177.      fputc('\n', fPrt );
  178.      fputc('(', fPrt );
  179.      while( *str )
  180.          {
  181.          if( (*str=='(') || (*str==')') || (*str=='\\') )
  182.                 fputc( '\\', fPrt );
  183.          fputc( *str, fPrt );
  184.          str++;
  185.          }
  186.      fputc(')', fPrt );
  187. }
  188.  
  189. /*-----------------------------------------------------------------*/
  190.  
  191. /* function to draw a right justified string at (x,y) */
  192.  
  193. void rjust_str( char *str, int x, int y )
  194. {
  195.      write_str( str );
  196.      fprintf( fPrt,
  197.               "\ndup stringwidth pop %d exch sub %d moveto show",
  198.               x, y );
  199. }
  200.  
  201. /*-----------------------------------------------------------------*/
  202.  
  203. /* function to draw a left justified string at (x,y) */
  204.  
  205. void ljust_str( char *str, int x, int y )
  206. {
  207.      write_str( str );
  208.      fprintf( fPrt, "\n%d %d moveto show", x, y );
  209. }
  210.  
  211. /*-----------------------------------------------------------------*/
  212.  
  213. /* function to draw a center justified string at (x,y) */
  214.  
  215. void cjust_str( char *str, int x, int y )
  216. {
  217.      write_str( str );
  218.      fprintf( fPrt,
  219.               "\ndup stringwidth pop 2 idiv %d exch sub %d moveto show",
  220.               x, y );
  221. }
  222.  
  223. /*-----------------------------------------------------------------*/
  224.  
  225. /* start a new page of text */
  226.  
  227. void start_pg( void )
  228. {
  229.      /* file name at upper left */
  230.      ljust_str( FileToPrint, XLft, YTop-PtSize );
  231.  
  232.      /* today's date at upper right */
  233.      rjust_str( DateAndTime, XRgt, YTop-PtSize );
  234.  
  235.      /* line across top */
  236.      draw_ln( XLft, YTop-LnSize-2, XRgt, YTop-LnSize-2 );
  237.  
  238.      /* set current position */
  239.      XCur = XLft;
  240.      YCur = YTop - 18 - PtSize;
  241. }
  242.  
  243. /*-----------------------------------------------------------------*/
  244.  
  245. /* finish up current page */
  246.  
  247. void finish_pg( void )
  248. {
  249.      char pg_lbl[16];
  250.  
  251.      /* line across bottom */
  252.      draw_ln( XLft, YBot+LnSize, XRgt, YBot+LnSize );
  253.  
  254.      /* page number at bottom center */
  255.      sprintf( pg_lbl, "- %d -", ++PageNo );
  256.      cjust_str( pg_lbl, (XRgt+XLft)/2, YBot );
  257.  
  258.      /* print the page */
  259.      fprintf( fPrt, "\nshowpage\n" );
  260. }
  261.  
  262. /*-----------------------------------------------------------------*/
  263.  
  264. /* main process loop */
  265.  
  266. void do_print( void )
  267. {
  268.      int           ic, oc, i;
  269.      char cur_iline[LINE_SIZE+1],
  270.           cur_oline[LINE_SIZE+1];
  271.  
  272.      /* initialize page coord variables */
  273.      XLft = 54;    XRgt = 558;
  274.      YTop = 738;   YBot = 54;
  275.  
  276.      /* how many lines per page ? */
  277.      LnPerPg = (YTop - YBot - HDR_HGT - FTR_HGT) / LnSize;
  278.  
  279.      /* print a comment as the first line out */
  280.      fprintf( fPrt, "%s", "%" );
  281.      fprintf( fPrt, "\n%s   PSP : printing '%s' on %s",
  282.               "%", FileToPrint, DateAndTime );
  283.      fprintf( fPrt, "\n%s", "%" );
  284.  
  285.      /* write code to select a font */
  286.      set_font();
  287.  
  288.      /* "prime" the loop */
  289.      cur_iline[LINE_SIZE] = 0;
  290.      LineNo = 0;
  291.      fgets( cur_iline, LINE_SIZE, fTxt );
  292.  
  293.      /* process each line in the file */
  294.      while( !feof( fTxt ) )
  295.           {
  296.           /* start of a new page? */
  297.           if( LineNo == 0 )
  298.               start_pg();
  299.  
  300.           ic = oc = 0;
  301.           while( !eoln( cur_iline[ic] ) )
  302.                {
  303.                /* expand a tab character */
  304.                if( cur_iline[ic] == TAB_CH )
  305.                    for( i=0; i<TAB_SIZE; i++ )
  306.                         cur_oline[oc++] = ' ';
  307.                /* ignore line feeds */
  308.                else if( cur_iline[ic] != LF_CH )
  309.                    cur_oline[oc++] = cur_iline[ic];
  310.                ic++;
  311.                }
  312.           cur_oline[oc] = 0;
  313.  
  314.           /* print the line */
  315.           if( oc > 0 )
  316.               ljust_str( cur_oline, XCur, YCur );
  317.  
  318.           /* update counters, positions */
  319.           YCur -= LnSize;
  320.           LineNo++;
  321.  
  322.           /* current page full? */
  323.           if( eopg( cur_iline[ic], LineNo ) )
  324.               {
  325.               finish_pg();
  326.               LineNo = 0;
  327.               }
  328.  
  329.           /* get another line */
  330.           fgets( cur_iline, LINE_SIZE, fTxt );
  331.           }
  332.  
  333.      /* finish any partial page at the end */
  334.      if( LineNo > 0 )
  335.          finish_pg();
  336.  
  337. #ifdef RIPS
  338.      fprintf( fPrt, "\004" );
  339. #endif
  340. }
  341.  
  342. /*-----------------------------------------------------------------*/
  343.  
  344. /* main... */
  345.  
  346. void main( int argc, char *argv[] )
  347. {
  348.      /* check command line */
  349.      if( (argc < 2) || (*argv[1] == '?') )
  350.           {
  351.           explain_pgm();
  352.           exit_pgm( "", 0 );
  353.           }
  354.  
  355.      /* identify ourselves */
  356.      printf( "\nPSP - PostScript File Printing Program" );
  357.  
  358.      /* set up... */
  359.      strcpy( FileToPrint, argv[1] );
  360.      strupr( FileToPrint );
  361.      if( argc > 2 )  strcpy( PrintDevice, argv[2] );
  362.      start_up( FileToPrint, PrintDevice );
  363.  
  364.      /* print the file */
  365.      printf( "\nPrinting '%s' to '%s'...", FileToPrint, PrintDevice );
  366.      do_print();
  367.  
  368.      /* clean up and exit */
  369.      shut_dn();
  370.      exit_pgm( "End of Pgm", 0 );
  371. }
  372.